package com.think.admin.oauth.handle;

import com.think.common.utils.JacksonUtils;
import com.think.common.utils.ResultUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限不足
 *
 * @author xieWenJie
 * @date 2018年12月17日
 */
@Component
public class OauthAccessDeniedHandlerImpl implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
            throws IOException {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(
                JacksonUtils.toJson(new ResultUtil(HttpStatus.FORBIDDEN, "请求失败：权限不足")));
    }
}
